package com.hr.controller;

import com.hr.pojo.*;
import com.hr.service.impl.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Author: WuJiwei
 * @Date: 2017/12/19 22:43
 * @Description: 用户Controller层
 */
@Controller
@RequestMapping("/hr-manager/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * @Author: WuJiwei
     * @Date: 2017/12/19 22:42
     * @Description: 用户登录
     * @Params: [userInfo, request]
     * @Return java.lang.String
     */
    @RequestMapping("/login")
    public String login(HrUserInfo userInfo, HttpServletRequest request) {
        System.err.println(userInfo.toString());
        HrUserInfoExample hrUserInfoExample = new HrUserInfoExample();
        HrUserInfoExample.Criteria criteria = hrUserInfoExample.createCriteria();
        criteria.andUsernameEqualTo(userInfo.getUsername().toString());
        criteria.andPasswordEqualTo(userInfo.getPassword().toString());
        List<HrUserInfo> hrUserInfos = userService.selectByExample(hrUserInfoExample);
        HrUserInfo hrUserInfo = hrUserInfos.size() == 0 ? null : userService.selectByExample(hrUserInfoExample).get(0);
        if (hrUserInfo == null) {
            request.getSession().setAttribute("msg", "用户名或密码错误,请重新登录!");
            return "err";
        }
        System.err.println("登录查询数据: " + hrUserInfo.toString());
        HttpSession session = request.getSession();
        session.setAttribute("username", hrUserInfo.getUsername());
        session.setAttribute("password", hrUserInfo.getPassword());
        return "manager";
    }

    /**
     * @Author: WuJiwei
     * @Date: 2017/12/19 22:42
     * @Description: 查询所有用户信息
     * @Params: []
     * @Return java.util.List<com.hr.pojo.UserInfos>
     */
    @RequestMapping("/findall.htm")
    @ResponseBody
    public List<UserInfos> findAll() {
        UserInfos userInfos = new UserInfos();
        HrUserInfoExample hrUserInfoExample = new HrUserInfoExample();
        List<HrUserInfo> hrUserInfos = userService.selectByExample(hrUserInfoExample);
        List<UserInfos> results = userService.findAllUserInfo(hrUserInfos);
        return results;
    }


    /**
     * @Author: WuJiwei
     * @Date: 2017/12/19 23:15
     * @Description: 根据用户id 删除用户,在真正开发中是build直接删除用户信息的, 而是改变用户的状态
     * @Params: [userId]
     * @Return com.hr.pojo.ResultInfo
     */
    @RequestMapping("/delectuser.html")
    @ResponseBody
    public ResultInfo delectUser(Integer userId) {
        ResultInfo resultInfo = userService.delectUserInfo(userId);
        return resultInfo;
    }

    /**
     * @Author: WuJiwei
     * @Date: 2017/12/19 23:18
     * @Description: 新增用户信息
     * @Params: []
     * @Return com.hr.pojo.ResultInfo
     */
    @RequestMapping("/adduserinfo")
    @ResponseBody
    public ResultInfo addUserInfo(HrUserInfo hrUserInfo, HrDept hrDept, HrStation hrStation) {
        System.err.println("新增用户信息如下:");
        System.err.println(hrUserInfo.toString());
        System.err.println(hrDept);
        System.err.println(hrStation);
        UserInfos userInfos = new UserInfos(hrUserInfo, hrDept, null, hrStation, null);
        ResultInfo resultInfo = userService.addUserInfo(userInfos);
        return resultInfo;
    }

    /**
     * @Author: WuJiwei
     * @Date: 2017/12/19 23:19
     * @Description: 编辑用户信息
     * @Params: []
     * @Return com.hr.pojo.ResultInfo
     */
    @RequestMapping("/updateuserinfo")
    @ResponseBody
    public ResultInfo updateUserInfo(HrUserInfo hrUserInfo, HrDept hrDept, HrStation hrStation) {
        System.err.println("修改用户信息如下:");
        System.err.println(hrUserInfo.toString());
        System.err.println(hrDept);
        System.err.println(hrStation);
        UserInfos userInfos = new UserInfos(hrUserInfo, hrDept, null, hrStation, null);
        ResultInfo resultInfo = userService.editUserInfo(userInfos);
        return resultInfo;
    }

}
